package com.WangNan.mall.service.impl;

import com.WangNan.mall.entity.Manager;
import com.WangNan.mall.exception.AccountExistException;
import com.WangNan.mall.mapper.ManagerMapper;
import com.WangNan.mall.service.ManagerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author :W.
 * @Date:2025/4/15 14:57
 */

@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public void add(Manager manager){

        //判断账号是否存在
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", manager.getAccount());

        Long count = managerMapper.selectCount(queryWrapper);
        //若存在，抛异常
        if (count > 0) {
            throw new AccountExistException("账号" + manager.getAccount() + "已存在！");
        }
        //否则添加到列表中
        managerMapper.insert(manager);
    }

    @Override
    public Manager login(String account, String password){
         /*QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        queryWrapper.eq("password", password);*/

        //封装查询条件
        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Manager::getAccount, account);
        queryWrapper.eq(Manager::getPassword, password);

        return managerMapper.selectOne(queryWrapper);
    }

}
